<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Model\CoinLog;
use App\Model\UserCoin;
use App\Model\UserCoinOpLog;
use App\Model\UserProfile;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Layout\Content;
use Illuminate\Support\MessageBag;

class UserCoinOpController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */

    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('用户虚拟币操作');
            $content->description('');
            $content->body($this->grid());
        });
    }

    /**
     * 编辑
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header('用户虚拟币操作');
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('用户虚拟币操作');
            $content->description('');
            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        // return Admin::grid(UserCoinOpLog::class, function (Grid $grid) {
        $grid = new Grid(new UserCoinOpLog());
        $grid->model()->where(['flag' => 1])->orderBy('id', 'desc');
        $grid->user_id('用户内部ID');
        $grid->userProfile()->user_profile_id('用户ID');
        $grid->userProfile()->nick_name('昵称');
        $grid->op_type('金币操作类型')->display(function ($data) {
            return UserCoinOpLog::OP_TYPE[$data];
        });
        $grid->coin('金币操作额度');
        $grid->created_at('操作时间')->display(function ($time) {
            return !$time ? '-' : date('Y-m-d H:i:s', strtotime($time));
        });
        $grid->op_reason('操作原因');
        // $grid->admin_id('操作人');

        //禁用导出
        $grid->disableExport();
        //禁用行选择
        $grid->disableRowSelector();
        $grid->filter(function ($filter) {
            // $filter->expand();
            $filter->disableIdFilter();
            $filter->equal('userProfile.user_profile_id', '用户ID');
            // $filter->equal('flag', '有效状态')->select([0 => '无效', 1 => '有效']);
        });
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableDelete();
            $actions->disableView();
            $actions->disableEdit();
        });
        // });
        return $grid;
    }

    /**
     * Make a form builder.
     * @return Form
     */
    protected function form()
    {
        return Admin::form(UserCoinOpLog::class, function (Form $form) {
            $form->text('user_profile_id', '用户ID');
            $form->select('op_type', '金币操作类型')->options(UserCoinOpLog::OP_TYPE);
            $form->text('coin', '操作金币金额');
            $form->text('op_reason', '操作金币原因');
            $form->ignore(['user_profile_id']);
            $form->hidden('user_id');
            $form->saving(function (Form $form) {
                $user = UserProfile::where('user_profile_id', request('user_profile_id'))->first();
                if (!$user) $this->error('没有该用户信息');
                if (!$form->coin) $this->error('金币数额错误！');
                $form->user_id = $user->user_id;
            });
            $form->disableEditingCheck();
            $form->disableCreatingCheck();
            $form->disableViewCheck();
            $form->saved(function (Form $form) {
                $model = $form->model();
                $coin = $model->coin;
                $userId = $model->user_id;
                try {
                    //操作金币
                    switch ($model->op_type) {
                            //扣除
                        case UserCoinOpLog::OP_TYPE_COST:
                            $userCoin = UserCoin::costCoin($userId, $coin);
                            CoinLog::addConsume($userId, $coin, CoinLog::FROM_SYS_COST, $userCoin);
                            break;
                            //补币
                        case UserCoinOpLog::OP_TYPE_GAIN:
                            $userCoin = UserCoin::gainIncome($userId, $coin);
                            CoinLog::addIncome($userId, $coin, CoinLog::FROM_SYS_GAIN, $userCoin);
                            break;
                    }
                } catch (\Exception $e) {
                    \Log::info(__CLASS__ . "=={$userId}=={$coin}==Exception:" . $e->getMessage());
                }
            });
        });
    }
}
